﻿#define UNICODE
#define _UNICODE
#include <jni.h>
#include <stdio.h>
#include <stdlib.h>
#include "com_singwine_hellondk_NativeLib.h"

JNIEXPORT void JNICALL Java_com_singwine_hellondk_NativeLib_DisplayHello (JNIEnv *, jobject)
{
	printf("from jni hello, world\n");
	return ;
}

JNIEXPORT jint JNICALL Java_com_singwine_hellondk_NativeLib_Add (JNIEnv *, jobject, jint a, jint b)
{
	return (a +b);
}

JNIEXPORT jstring JNICALL Java_com_singwine_hellondk_NativeLib_strAdd (JNIEnv *env, jobject, jstring a, jstring b)
{
	jboolean bCopy = false;
	const jchar* str1 = env->GetStringChars(a, &bCopy);
	jsize len1 = env->GetStringLength(a);
	const jchar* str2 = env->GetStringChars(b, &bCopy);
	jsize len2 = env->GetStringLength(b);
	jchar* str = new jchar[len1 + len2 + 1];
	str[len1+len2] = 0x00;
	memcpy(str, str1, len1*2);
	memcpy(str + len1, str2, len2*2);
	env->ReleaseStringChars(a, str1);
	env->ReleaseStringChars(b, str2);
	jstring jstr = env->NewString(str, len1 + len2);
	delete[] str;
	return jstr;
}

JNIEXPORT void JNICALL Java_com_singwine_hellondk_NativeLib_callFoo(JNIEnv * env, jobject obj)
{
	printf("in testjni callfoo\n");
	jclass cls = env->GetObjectClass(obj);
	jmethodID methodId = env->GetMethodID(cls, "foo", "(ILjava/lang/String;)V");
	jint times = 3;
	jstring str = env->NewString((const jchar*)L"谢谢你", 3);
	env->CallVoidMethod(obj, methodId, times, str);
}
